package cool.taomu.toolkit.demo.stock.listener

import cool.taomu.toolkit.demo.stock.listener.entity.StockDataEvent

class MacdSignalListener extends BaseSignalListener {

	override void process(StockDataEvent event) {
		var data = event.getHistoryList();
		var  code = event.getStockCode();

		for (var int i = 2; i < data.size(); i++) {
			var c = data.get(i);
			var p = data.get(i - 1);
			var b = data.get(i - 2);

			if (isBottom(c, p, b)) {
				var double cv = c.macd
				var double pv = p.macd
				if (cv > pv && pv <= b.macd) {
					saveSignal(code, c.getDate(), "MACD", "BUY", c.getClose(), cv);
				}
			}

			if (isTop(c, p, b)) {
				var double cv = c.macd
				var double pv = p.macd
				if (cv < pv && pv >= b.macd) {
					 saveSignal(code, c.getDate(), "MACD", "SELL", c.getClose(), cv);
				}
			}
		}
	}
}
